package camera.bean;

import android.app.Activity;
import android.opengl.GLES11;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import camera.CameraData;
import camera.CameraHolder;
import camera.OpenGl.CameraShaderProgram;
import camera.OpenGl.utils.ByteBufferUtils;
import camera.OpenGl.utils.GLUtils;

/**
 *@author:ike
 *@function: 相机图像预览数据实体类
 *@creat time:15:23 2017/12/29
 **/
public class CameraPic    {
    
    private CameraShaderProgram program;
  
    /**
     * 位置坐标+纹理坐标
     */
    private float[] vertexCoord={
            -1.0f, 1.0f, 0.0f,0.0f, 1.0f,
            -1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
            1.0f, -1.0f, 0.0f,1.0f, 0.0f,
            1.0f, 1.0f, 0.0f,0.0f, 0.0f
    };
    
    private FloatBuffer positionBuffer;
    
    public CameraPic(CameraShaderProgram program) {
        this.program=program;
        positionBuffer= ByteBufferUtils.makeFloteByteBuffer(vertexCoord);
        bindData(program);
    }
    public void bindData(CameraShaderProgram program){
        GLES20.glEnableVertexAttribArray(program.vPosition);
        positionBuffer.position(0);
        GLES20.glVertexAttribPointer(program.vPosition,3,GLES20.GL_FLOAT,false,
                20,positionBuffer);
        positionBuffer.position(3);
        GLES20.glEnableVertexAttribArray(program.inputTextureCoordinate);
        GLES20.glVertexAttribPointer(program.inputTextureCoordinate,2,GLES20.GL_FLOAT,false,
                20,positionBuffer);
    }

    /**
     * 绘制
     */
    public void  draw(float[] matrix,int textureId){
        program.useProgram();
        //传递位置点的变换矩阵给着色器
        GLES20.glUniformMatrix4fv(program.vMatrix,1,false,matrix,0);
        //获取渲染图像的纹理单位
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        //绑定纹理单位
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,textureId);
        //将获取到的纹理单位，s_texture片元着色器进行图像渲染显示
        GLES20.glUniform1i(program.s_texture,0);
        //开始绘制
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN,0,4);
       // GLES20.glDrawArrays(GLES20.GL_TRIANGLES,3,3);
    }
    
}
